package vn.com.bookstore.util.transformer;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class NonSwipeViewPager extends ViewPager {
	public static final int SCROLL_LEFT = -1;
	public static final int SCROLL_RIGHT = 1;
	boolean canScrollLeft = true;
	boolean canScrollRight = true;

	public NonSwipeViewPager(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public NonSwipeViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public void setCanScrollHorizontally(int direction, boolean canScroll) {
		switch (direction) {
		case SCROLL_LEFT:
			canScrollLeft = canScroll;
			break;
		case SCROLL_RIGHT:
			canScrollRight = canScroll;
			break;
		default:
			break;
		}
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	public boolean canScrollHorizontally(int direction) {
		if (direction < 0) { // left
			return canScrollLeft;
		} else {
			return canScrollRight;
		}
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		if (!canScrollLeft && !canScrollRight)
			// Never allow swiping to switch between pages
			return false;
		return super.onInterceptTouchEvent(arg0);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!canScrollLeft && !canScrollRight)
			// Never allow swiping to switch between pages
			return false;
		return super.onTouchEvent(event);
	}
}
